package com.zhijian.warehouse.manage.tcp;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;

import java.nio.charset.StandardCharsets;
import java.util.List;

public class PackageDecoder extends ReplayingDecoder<Void> {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
        MyPackage pkg = new MyPackage();
        String start = in.readCharSequence(2, StandardCharsets.UTF_8).toString();
        String length = in.readCharSequence(4, StandardCharsets.UTF_8).toString();
        String data = in.readCharSequence(Integer.parseInt(length), StandardCharsets.UTF_8).toString();
        String crcHex = in.readCharSequence(4, StandardCharsets.UTF_8).toString();
        String end = in.readCharSequence(2, StandardCharsets.UTF_8).toString();

        pkg.setStart(start);
        pkg.setLength(length);
        pkg.setData(data);
        pkg.setCrcHex(crcHex);
        pkg.setEnd(end);
        out.add(pkg);
    }

    public static void main(String[] args) {
        String s = "##0370QN=20231206093100000;ST=22;CN=2011;PW=123456;MN=HSTCHR22112202;Flag=4;CP=&&DataTime=20231206093000;a01001-Rtd=0.00,a01001-Flag=N;a01002-Rtd=0.00,a01002-Flag=N;a01006-Rtd=35.72,a01006-Flag=N;a01007-Rtd=0.00,a01007-Flag=N;a01008-Rtd=67.50,a01008-Flag=N;a34004-Rtd=0.00,a34004-Flag=N;a34002-Rtd=311.03,a34002-Flag=N;a34001-Rtd=0.00,a34001-Flag=N;Leq-Rtd=65.38,Leq-Flag=N&&2700";
    }

}
